Ext.define('YXMES.sres.controller.DeviceGroupSetupController', {
	extend : 'Ext.app.Controller',
	refs : [ {
		selector : 'sres_devicegroupsetup',
		ref : 'sresdevicegroupsetup'
	} ],
	requires : [ 'YXMES.sres.store.ResourceGroupList', 'YXMES.sres.store.ResourceList', 'YXMES.sres.store.GroupResList', 'YXMES.sres.store.ResGroupDefList', 'YXMES.sres.store.WorkshopStore', 'YXMES.sres.store.ResTypeStore' ],
	stores : [],
	models : [],
	views : [ 'YXMES.sres.view.DeviceGroupSetupView' ],
	subcontrollers : [  ],
	_init : false,
	init : function() {
		if (this._init)
			return;
		this._init = true;
		this.control({
			'sres_devicegroupsetup' : {
				afterrender : function() {
					return this.handle(this.onAfterrender, arguments);
				},
				destroy : function() {
					return this.handle(this.onDestroy, arguments);
				}
			},
			'sres_devicegroupsetup #cmbGroupCode' : {
				select : function() {
					return this.handle(this.onCmbGroupCodeSelect, arguments);
				}
			},
			'sres_devicegroupsetup #btnSearch' : {
				click : function() {
					return this.handle(this.onBtnSearchClick, arguments);
				}
			},
			'sres_devicegroupsetup #btnGoLeft' : {
				click : function() {
					return this.handle(this.onBtnGoLeftClick, arguments);
				}
			},
			'sres_devicegroupsetup #btnGoRight' : {
				click : function() {
					return this.handle(this.onBtnGoRightClick, arguments);
				}
			},
			'sres_devicegroupsetup #btnSave' : {
				click : function() {
					return this.handle(this.onBtnSaveClick, arguments);
				}
			},
			'sres_devicegroupsetup #btnDel' : {
				click : function() {
					return this.handle(this.onBtnDelClick, arguments);
				}
			}
		});
		if (!Ext.isEmpty(this.subcontrollers)) {
			Ext.each(this.subcontrollers, function(name) {
				this.getController(name).init();
			}, this);
		}
		// User definied init
	},
	mainView : undefined,
	getMainView : function() {
		return this.mainView;
	},
	getScopeView : function() {
		return this.getMainView().scopeView || {};
	},
	getInput : function() {
		return this.getScopeView().input || {};
	},
	setInput : function(data) {
		this.getMainView().input = data;
	},
	getOutput : function() {
		return this.getMainView().output || {};
	},
	setOutput : function(data) {
		this.getScopeView().output = data;
	},
	handle : function(func, args) {
		var item = args && args.length > 0 && Ext.typeOf(args[0]) === 'object' ? args[0] : undefined;
		if (item) {
			try {
				this.mainView = (item.xtype == 'sres_devicegroupsetup' ? item : item.up('sres_devicegroupsetup'));
			} catch(e) {
			}
		}
		try {
			return func.apply(this, args);
		} finally {
		}
	},
	getItem : function(itemId) {
		return this.getMainView().down('#' + itemId);
	},
	getStore : function(name) {
		if (name == 'ResourceGroupList')
			return this.getMainView().SresViewGroupInfoServiceStore;
		if (name == 'ResourceList')
			return this.getMainView().SresViewDeviceResourceServiceStore;
		if (name == 'GroupResList')
			return this.getMainView().SresViewGroupResourceDetailServiceStore;
		if (name == 'ResGroupDefList')
			return this.getMainView().SresViewGroupInfoServiceStore2;
		if (name == 'WorkshopStore')
			return this.getMainView().SbasViewWorkshopServiceStore;
		if (name == 'ResTypeStore')
			return this.getMainView().BasViewCommonDataSetupDetailServiceStore;
		return undefined;
	},
	bind : function(view) {
		view = (view ? view : this.getMainView());
		view.SresViewGroupInfoServiceStore = Ext.create('YXMES.sres.store.ResourceGroupList', {
			pageSize : 30,
			autoDestory : true,
			autoLoad : true
		});
		view.SresViewDeviceResourceServiceStore = Ext.create('YXMES.sres.store.ResourceList', {
			pageSize : 30
		});
		view.SresViewGroupResourceDetailServiceStore = Ext.create('YXMES.sres.store.GroupResList', {
			pageSize : 30
		});
		view.SresViewGroupInfoServiceStore2 = Ext.create('YXMES.sres.store.ResGroupDefList', {
			pageSize : 30,
			autoDestory : true,
			autoLoad : true
		});
		view.SbasViewWorkshopServiceStore = Ext.create('YXMES.sres.store.WorkshopStore', {
			pageSize : 30,
			autoDestory : true,
			autoLoad : true
		});
		view.BasViewCommonDataSetupDetailServiceStore = Ext.create('YXMES.sres.store.ResTypeStore', {
			pageSize : 30
		});
		view.down('#grdRes').reconfigure(view.SresViewDeviceResourceServiceStore);
		view.down('#bar50').bindStore(view.SresViewGroupInfoServiceStore);
		view.down('#grdBoundEquipment').reconfigure(view.SresViewGroupResourceDetailServiceStore);
		view.down('#cmbGroupCode').bindStore(view.SresViewGroupInfoServiceStore2);
		view.down('#cmbShopCode').bindStore(view.SbasViewWorkshopServiceStore);
		view.down('#cmbResType').bindStore(view.BasViewCommonDataSetupDetailServiceStore);
	},
	onAfterrender : function(view, eOpts) {
		this.bind();
		// User defined afterrender
		this.loadResTypeStore({
		    groupName: 'RES_TYPE'
		});
		//this.loadWorkshopStore();
	},
	onDestroy : function(view, eOpts) {
		// User defined destory
		var view = this.getMainView();
		if (view.SresViewGroupInfoServiceStore) {
			view.SresViewGroupInfoServiceStore.destroyStore();
			view.SresViewGroupInfoServiceStore = undefined;
		}
		if (view.SresViewDeviceResourceServiceStore) {
			view.SresViewDeviceResourceServiceStore.destroyStore();
			view.SresViewDeviceResourceServiceStore = undefined;
		}
		if (view.SresViewGroupResourceDetailServiceStore) {
			view.SresViewGroupResourceDetailServiceStore.destroyStore();
			view.SresViewGroupResourceDetailServiceStore = undefined;
		}
		if (view.SresViewGroupInfoServiceStore2) {
			view.SresViewGroupInfoServiceStore2.destroyStore();
			view.SresViewGroupInfoServiceStore2 = undefined;
		}
		if (view.SbasViewWorkshopServiceStore) {
			view.SbasViewWorkshopServiceStore.destroyStore();
			view.SbasViewWorkshopServiceStore = undefined;
		}
		if (view.BasViewCommonDataSetupDetailServiceStore) {
			view.BasViewCommonDataSetupDetailServiceStore.destroyStore();
			view.BasViewCommonDataSetupDetailServiceStore = undefined;
		}
		if (this.getMainView().input)
			this.getMainView().input = undefined;
		if (this.getMainView().output)
			this.getMainView().output = undefined;
	},
	onCmbGroupCodeSelect : function(combo, records, eOpts) {
		this.getItem('txtGroupDesc').setValue(records[0].get('groupDesc'));
		this.loadGroupResList({
		    groupCode: records[0].get('groupCode')
		});
	},
	onBtnSearchClick : function(button, e, eOpts) {
		this.loadResourceList(this.getItem('frmResSearch').getValues());
	},
	onBtnGoLeftClick : function(button, e, eOpts) {
		var rcds = this.getItem('grdRes').getSelectionModel().getSelection();
		if (Ext.isEmpty(rcds)) {
		    return;
		}
		
		var destStore = this.getItem('grdBoundEquipment').getStore();
		Ext.each(rcds,
		function(rcd) {
		    if (destStore.findRecord('resId', rcd.get('resId')) === null) {
		        destStore.insert(0, rcd);
		    }
		});
		this.getItem('grdRes').getSelectionModel().deselectAll();
	},
	onBtnGoRightClick : function(button, e, eOpts) {
		var rcds = this.getItem('grdBoundEquipment').getSelectionModel().getSelection();
		if (Ext.isEmpty(rcds)) {
		    return;
		}
		
		this.getItem('grdBoundEquipment').getStore().remove(rcds);
	},
	onBtnSaveClick : function(button, e, eOpts) {
		if (this.getItem('cmbGroupCode').getValue()==="") {
		    return;
		}
		
		var st = this.getItem('grdBoundEquipment').getStore();
		var detail=[];
		st.each(function(rcd){
		    detail.push({
		        resId:rcd.get('resId')
		    });
		});
		
		var parm = {
		    groupCode:this.getItem('cmbGroupCode').getValue(),
		    groupDesc:this.getItem('txtGroupDesc').getValue(),
		    status:"N",
		    resList:detail
		};
		
		Ext.Msg.show({
		    title: button.getText(),
		    msg: T.msg.are_you_sure_to_save_data,
		    icon: Ext.Msg.QUESTION,
		    buttons: Ext.Msg.YESNO,
		    fn: function(btnId) {
		        if ('yes' == btnId) {
		            button.setDisabled(true);
		
		            YX.ZbusRpc.request({
		                module: 'SresSaveGroupInfoAndDetailService',
		                params: parm,
		                showSuccess: true,
		                callback: function() {
		                    button.setDisabled(false);
		                }
		            })
		        }
		    }
		});
	},
	onBtnDelClick : function(button, e, eOpts) {
		if (this.getItem('cmbGroupCode').getValue() === "") {
		    return;
		}
		
		var parm = {
		    groupCode: this.getItem('cmbGroupCode').getValue(),
		    status: "D"
		};
		
		var me = this;
		Ext.Msg.show({
		    title: button.getText(),
		    msg: T.msg.are_you_sure_to_delete_data,
		    icon: Ext.Msg.QUESTION,
		    buttons: Ext.Msg.YESNO,
		    fn: function(btnId) {
		        if ('yes' == btnId) {
		            button.setDisabled(true);
		
		            YX.ZbusRpc.request({
		                module: 'SresSaveGroupInfoAndDetailService',
		                params: parm,
		                showSuccess: true,
		                callback: function() {
		                    button.setDisabled(false);
		                    me.getItem('cmbGroupCode').setValue('');
		                    me.getItem('txtGroupDesc').setValue('');
		                    me.getItem('grdBoundEquipment').getStore().removeAll();
		                    me.loadResGroupDefList();
		                }
		            })
		        }
		    }
		});
	},
	loadResourceGroupList : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('ResourceGroupList');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadResourceList : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('ResourceList');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadGroupResList : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('GroupResList');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadResGroupDefList : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('ResGroupDefList');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadWorkshopStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('WorkshopStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadResTypeStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('ResTypeStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	}
});